package org.graduation.project.controller;

import org.graduation.common.NoPermissionException;
import org.graduation.feignapi.RBACClient;
import org.graduation.feignapi.UserClient;
import org.graduation.model.common.RBAC.Permissions;
import org.graduation.model.common.ResponseResult;
import org.graduation.model.common.project.Phase;
import org.graduation.model.dtos.RBAC.CheckPermissionsDTO;
import org.graduation.project.mapper.MybatisPlusWrapperFactory;
import org.graduation.project.service.PhaseService;
import org.graduation.project.service.ProjectService;
import org.graduation.utils.ResponseResultUtil;
import org.graduation.utils.Token;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/project/phase")
public class PhaseController {
    @Autowired
    private PhaseService phaseService;
    @Autowired
    private ProjectService projectService;
    @Autowired
    private ResponseResultUtil responseResultUtil;
    @Autowired
    private Token token;
    @Autowired
    private UserClient userClient;
    @Autowired
    private RBACClient rbacClient;

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public ResponseResult create(@RequestBody Phase phase, HttpServletRequest request) {
        Permissions permission = Permissions.CREATE_PHASE;
        if (!checkPermissions(phase, request, permission)) {
            throw new NoPermissionException(permission);
        }
        return ResponseResultUtil.simpleCheck(phaseService.save(phase), "创建失败请稍候再试。");
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ResponseResult update(@RequestBody Phase phase, HttpServletRequest request) {
        Permissions permission = Permissions.UPDATE_PHASE_INFO;
        if (!checkPermissions(phase, request, permission)) {
            throw new NoPermissionException(permission);
        }
        return ResponseResultUtil.simpleCheck(phaseService.updateById(phase));
    }

    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public ResponseResult delete(@RequestBody Phase phase, HttpServletRequest request) {
        Permissions permission = Permissions.DELETE_PHASE;
        if (!checkPermissions(phase, request, permission)) {
            throw new NoPermissionException(permission);
        }
        //todo delete all task
        return ResponseResultUtil.simpleCheck(phaseService.removeById(phase));
    }

    /**
     * 获取项目所有的phase
     *
     * @param pid
     * @param request
     * @return
     */
    @RequestMapping(value = "/view", method = RequestMethod.GET)
    public ResponseResult view(@RequestParam Long pid, HttpServletRequest request) {
        Permissions permission = Permissions.VIEW_PHASE;
        int uid = token.getUidByToken(token.getTokenByRequest(request));
        if (!checkPermissions(pid, uid, permission)) {
            throw new NoPermissionException(permission);
        }
        List<Phase> list = phaseService.list(MybatisPlusWrapperFactory.getAllPhaseByPId(pid));
        return ResponseResult.success(list);
    }

    protected boolean checkPermissions(Long pId, Integer uid, Permissions permission) {
        CheckPermissionsDTO dto = new CheckPermissionsDTO(pId, uid, permission);
        return rbacClient.check(dto).getCode() == 200;
    }

    protected boolean checkPermissions(Phase phase, HttpServletRequest request, Permissions permission) {
        int uid = token.getUidByToken(token.getTokenByRequest(request));
        return checkPermissions(phase.getPId(), uid, permission);
    }
}
